package com.huawei.bus.minitest;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.json.JSONException;

import com.huawei.bus.minitest.conf.Conf;
import com.huawei.bus.minitest.constant.ConfigConstant;
import com.huawei.bus.minitest.err.TestError;
import com.huawei.bus.minitest.exe.IExecute;

public class TestSuite {
	private String suitesDirPath;
	private Map<String,ArrayList<TestCase>> testSuiteMap;
	private ArrayList<TestError> errors = new ArrayList<TestError>();
	
	public TestSuite() {
		suitesDirPath = Conf.glob.getTestsuitePath();
		testSuiteMap = new HashMap<String,ArrayList<TestCase>>();
	}

	public void loadTestSuites() {
		File testSuiteFile = new File(suitesDirPath);
		if(testSuiteFile.exists())
		{
			String rootDir = testSuiteFile.getName();
			ArrayList<TestCase> rootTestList = new ArrayList<TestCase>();
			testSuiteMap.put(rootDir, rootTestList);
			File[] tsFiles = testSuiteFile.listFiles();
			for (File file : tsFiles) {
				String fileName = file.getName();
				if(file.isFile()){
					//if is *.json file, suppose it's testcase file
					if(fileName.endsWith("."+ConfigConstant.JSON_FILE_POST_FIX)){
						TestCase tc = null;
						try {
							tc = new TestCase(file,rootDir);
						} catch (JSONException e) {
							e.printStackTrace();
							errors.add(new JsonParseError(rootDir,fileName,file.getAbsolutePath(),e));
						}
						rootTestList.add(tc);
					}
				}else{
					//if test case directory, list all test case files, like test1.json,test2,json...
					ArrayList<TestCase> testcaseList = new ArrayList<TestCase>();
					File[] tcFiles = file.listFiles();
					for (File testcaseFile : tcFiles) {
						//if directory, not process the dir now
						if(testcaseFile.isDirectory()) continue;
						
						String tcName = testcaseFile.getName();
						if(tcName.endsWith("."+ConfigConstant.JSON_FILE_POST_FIX)){
							TestCase tc = null;
							try {
								tc = new TestCase(testcaseFile,fileName);
							} catch (JSONException e) {
								e.printStackTrace();
								errors.add(new JsonParseError(fileName,tcName,testcaseFile.getAbsolutePath(),e));
							}
							testcaseList.add(tc);
						}
					}
					testSuiteMap.put(fileName, testcaseList);
				}
			}
		}
	}

	public void runAll() {
		 Set<Entry<String, ArrayList<TestCase>>> testcases = testSuiteMap.entrySet();
		 for (Entry<String, ArrayList<TestCase>> entry : testcases) {
			 ArrayList<TestCase> testcaseList = entry.getValue();
			 System.out.println("begin running testcase:" + entry.getKey());
			 for (Iterator iterator = testcaseList.iterator(); iterator
					.hasNext();) {
				TestCase testCase = (TestCase) iterator.next();
				Map context = new HashMap();
				ArrayList<IExecute> steps = testCase.getSteps();
				for (Iterator iterator2 = steps.iterator(); iterator2.hasNext();) {
					IExecute iExecute = (IExecute) iterator2.next();
					context = iExecute.execute(context);
				}
			}
			System.out.println("end running testcase:" + entry.getKey());
		}
	}

}
